package org.argeproje.resim.proc.tools;

import java.util.List;
import java.util.Iterator;

import org.argeproje.resim.proc.Processor;
import org.argeproje.resim.ui.model.ComponentItem;
import org.argeproje.resim.ui.model.Connection;
import org.argeproje.resim.ui.model.Shape;
import org.argeproje.resim.ui.model.ShapesDiagram;


public class ShapeDiagramRunner {
	private static ShapesDiagram _shapesDiagram;
	public static void setShapesDiagram(ShapesDiagram shapesDiagram){
		_shapesDiagram = shapesDiagram;
	}
	public static void updateProcessors(ShapesDiagram diagram){
		List<Shape> shapes = diagram.getChildren();
	    for (Iterator<Shape> iterator = shapes.iterator(); iterator.hasNext();) {
			ComponentItem ci = (ComponentItem) iterator.next();
			ci.updateProc();	
		}
	}
	public static void run(){
		ProcessorGraph.getInstance().clearGraph();
		List<Shape> shapes = _shapesDiagram.getChildren();
	    for (Iterator<Shape> iterator = shapes.iterator(); iterator.hasNext();) {
			Shape shape = (Shape) iterator.next();
			List<org.argeproje.resim.ui.model.Connection> conns = shape.getSourceConnections();
			for (Iterator<Connection> iterator2 = conns.iterator(); iterator2.hasNext();) {
				org.argeproje.resim.ui.model.Connection connection = 
					(org.argeproje.resim.ui.model.Connection) iterator2.next();
				Processor srcProc = ((ComponentItem)connection.getSource()).getProcessor();
				Processor dstProc = ((ComponentItem)connection.getTarget()).getProcessor();
				int srcIndex = connection.getSrcIndex();
				int dstIndex = connection.getDstIndex();
				ProcessorGraph.getInstance().newConnection(srcProc, srcIndex, dstProc, dstIndex);
			}
		}
	    ProcessorGraph.getInstance().run();
	}
}
